{% extends 'AdminBundle::layout.html.twig' %}

{% block title %}企业信息列表{% endblock %}

{% block css %}
    <style>
        .mes-input{
            margin-bottom: 10px;
            width: 23%;
        }
        .mes-select{
            width: 23.2%;
        }
        input{
            width: 90%;
        }
        .upload-import{
            position: relative;
            overflow: hidden;
            display: block;
            float: left;
        }
        #import_file{
            position: absolute;
            width: 80px;
            height: 35px;
            opacity: 0;
        }
    </style>
{% endblock %}


{% block top %}
    {#{{ dump() }}#}
    {{ parent() }}
{% endblock %}



    {% block left_body_top %}
        {{ parent() }}
    {% endblock %}

    {% block left_body %}
        {{ parent() }}
    {% endblock %}

    {% block right_body %}
    <div class="page-header">
        <div class="page-right-top">
            <div class="right-top">
                <span class="right-title">企业信息管理</span>
            </div>
        </div>
        <div class="main-content">
            <div class="main-top">
                <div class="index-list index-m1">
                    <div class="index-total">企业总数</div>
                    <div class="index-totalNum">
                        <span class="all-company-count"></span>个
                    </div>
                </div>
                <div class="index-list qy-m2">
                    <div class="index-total">今日新增企业数</div>
                    <div class="index-totalNum">
                        <span class="today-company-count"></span>个
                    </div>
                </div>
            </div>

            <div class="mes-content">
                <div class="main-mes-top">


                    {#<div class="fr">#}
                        <div class="mes-input fl">
                            <input type="text" class="keyword-input" name="select_credit_code" placeholder="请输入统一社会信用代码">
                        </div>
                        <div class="mes-input fl">
                            <input type="text" class="keyword-input" name="select_name" placeholder="请输入企业名称">
                        </div>

                        <div class="mes-input fl">
                            <input type="text" class="keyword-input" name="select_fr_name" placeholder="请输入法人姓名">
                        </div>

                        <div class="mes-input fl">
                            <input type="text" class="keyword-input" name="select_user_name" placeholder="请输入科技联络人姓名">
                        </div>

                        <select name="select_company_attribute_id" class="mes-select fl">
                            <option value="">请选择企业属性</option>
                            {% for item in data.attribute %}
                            <option value="{{ item.id }}">{{ item.company_attribute_name }}</option>
                            {% endfor %}
                        </select>

                        <select name="select_company_category_id" class="mes-select fl">
                            <option value="">请选择企业类别</option>
                            {% for item in data.category %}
                            <option value="{{ item.id }}">{{ item.company_category_name }}</option>
                            {% endfor %}
                        </select>

                        <select name="select_admin_id" class="mes-select fl">
                            <option value="">请选择科技载体</option>
                            {% for item in data.teac_admin %}
                            <option value="{{ item.id }}">{{ item.nickname }}</option>
                            {% endfor %}
                        </select>




                        <button class="mes-query-btn fl">查询</button>
                    {#</div>#}
                    <div class="clearfix"></div>
                </div>

                <div class="main-mes-content">
                    <div class="al-released fl">企业列表</div>
                    <div class="acc-operation fr">
                        <div class="fl">
                            <a class="release-btn fl" style="width:140px;margin-right: 40px;" href="{{ asset('excelModel.xlsx') }}">
                                <div class="train-width">
                                    <span class="qy-down-icon fl"></span>
                                    <span class="re-text fl">导入模板下载</span>
                                </div>
                            </a>
                        </div>
                        <div class="upload-import"  style="position:relative">
                            <input type="file" id="import_file">
                            <div class="acc-opera-btn fl enable-btn">导入</div>
                        </div>

                        <div class="acc-opera-btn fl disable-btn">导出</div>
                    </div>

                    <table class="table company-table" style="table-layout:fixed;">
                        <thead>
                        <tr style="background-color: #f3f9fd;">
                            <th class="center-th">序号</th>
                            <th class="center-th">企业名称</th>
                            <th class="center-th">企业属性</th>
                            <th class="center-th">企业类别</th>
                            <th class="center-th">统一社会信用代码</th>
                            <th class="center-th">科技载体</th>
                            <th class="center-th">科技联络人</th>
                            <th class="center-th">详情</th>
                            <th class="center-th">操作</th>
                        </tr>
                        </thead>
                        <tbody>


                        </tbody>
                    </table>

                    <div class="page-box">
                        <ul class="pagination">

                        </ul>
                    </div>
                </div>
            </div>
        </div>
    </div>


    <div class="pop-status opacity"></div>


    {% endblock %}


{% block box %}

    <!--编辑企业信息-->
    <div class="pop-eqit-mes">
        <div class="pop-title">
            编辑企业信息
            <span class="close close-icon"></span>
        </div>
        <form id="comMesBtn">
            <div class="com-list">
                <div class="com-list-box">
                    <div class="com-left fl">勋章</div>
                    <select class="com-select" name="medal_id">
                        <option value="">请选择勋章类型</option>
                        {% for item in data.medal %}
                        <option value="{{ item.id|default('') }}">{{ item.medal_name|default('') }}</option>
                        {% endfor %}
                    </select>
                </div>

                <input type="hidden" name="id" value="">

                <div class="com-list-box">
                    <div class="com-left fl">事件类型</div>
                    <select class="com-select" name="event_type">
                        <option value="">请选择事件类型</option>
                        <option value="1">奖励事件</option>
                        <option value="2">一般事件</option>
                        <option value="3">不良事件</option>
                    </select>
                </div>

                <div class="com-list-box">
                    <div class="com-left fl">事件内容</div>
                    <input type="text" name="event_content" class="pop-com-input" maxlength="50" placeholder="请输入事件内容">
                    <span class="pop-com-num">50</span>
                </div>

                <div class="display">
                    <div class="com-list-box fl">
                        <div class="com-left fl">科技载体</div>
                        <select class="com-select" name="admin_id">
                            <option value="">请选择科技载体</option>
                            {% for item in data.teac_admin %}
                            <option value="{{ item.id }}">{{ item.nickname }}</option>
                            {% endfor %}
                        </select>
                    </div>

                    <div class="com-list-box fl">
                        <div class="com-left fl" style="width: 66px;">企业类别</div>
                        <select class="com-select" name="company_category_id">
                            <option value="">请选择企业类别</option>
                            {% for item in data.category %}
                            <option value="{{ item.id }}">{{ item.company_category_name }}</option>
                            {% endfor %}
                        </select>
                    </div>
                </div>

                <div class="display">
                    <div class="com-list-box fl">
                        <div class="com-left fl">企业属性</div>
                        <select class="com-select" name="company_attribute_id">
                            <option value="">请选择企业属性</option>
                            {% for item in data.attribute %}
                            <option value="{{ item.id }}">{{ item.company_attribute_name }}</option>
                            {% endfor %}

                        </select>
                    </div>

                </div>

                <div class="clearfix"></div>
                <div class="add-mem-box" style="margin-top: 40px;">
                    <div class="com-left fl"></div>
                    <div class="acc-opera-btn fl mem-btn p-first-btn">保存</div>
                    <div class="acc-opera-btn fl mem-cancel">取消</div>
                </div>
            </div>
        </form>
    </div>


    <!--科技联络人-->
    <div class="pop-second pop-liaison" style="display: none;">
        <div class="pop-title">
            科技联络人
            <span class="close close-icon"></span>
        </div>
        <div style="padding: 20px 30px;">
            <table class="table technolog-table" style="table-layout:fixed;">
                <thead>
                <tr style="background-color: #f3f9fd;">
                    <th class="center-th" width="20%;">姓名</th>
                    <th width="30%;">手机号</th>
                    <th>邮箱</th>
                </tr>
                </thead>
                <tbody>



                </tbody>
            </table>
        </div>
    </div>


    <!--企业分类信息-->
    <div class="pop-second pop-classify" style="display: none;">
        <div class="pop-title">
            选择企业分类信息
            <span class="close close-icon"></span>
        </div>
        <form id="classSubmit">
            <div style="padding: 20px 30px;">
                <div class="com-list-box">
                    <div class="com-left fl" style="width: 66px;">企业属性</div>
                    <select class="com-select" name="qysx" style="width: 380px;">
                        <option value="">请选择企业属性</option>
                        {% for item in data.attribute %}
                            <option value="{{ item.id }}">{{ item.company_attribute_name }}</option>
                        {% endfor %}
                    </select>
                </div>

                <div class="com-list-box">
                    <div class="com-left fl" style="width: 66px;">企业类别</div>
                    <select class="com-select" name="qylb" style="width: 380px;">
                        <option value="">请选择企业类别</option>
                        {% for item in data.category %}
                            <option value="{{ item.id }}">{{ item.company_category_name }}</option>
                        {% endfor %}
                    </select>
                </div>

                <div class="add-mem-box" style="margin:30px 0;overflow: hidden;position:relative">
                    <div class="com-left fl"></div>

                    <div class="acc-opera-btn fl mem-btn classify-btn">保存</div>
                    <div class="acc-opera-btn fl mem-cancel">取消</div>
                </div>
            </div>
        </form>
    </div>


{% endblock %}


{% block javascript %}
    {{ parent() }}
<script type="text/javascript">


    SidebarHighlighting('4');

    // 点击查询
    var select_credit_code = "";
    var select_name = "";
    var select_fr_name = "";
    var select_user_name = "";
    var select_company_attribute_id = "";
    var select_company_category_id = "";
    var select_admin_id = "";

    $('.mes-query-btn').on('click', function() {
        select_credit_code = $("input[name='select_credit_code']").val();
        select_name = $("input[name='select_name']").val();
        select_fr_name = $("input[name='select_fr_name']").val();
        select_user_name = $("input[name='select_user_name']").val();
        select_company_attribute_id = $("select[name='select_company_attribute_id']").val();
        select_company_category_id = $("select[name='select_company_category_id']").val();
        select_admin_id = $("select[name='select_admin_id']").val();

        page_times = 0;
        addSearchAction(1);
    });

    // 删除，恢复
    $('body').on('click','.company-status', function() {

        var id = $(this).data("id");
        var status = $(this).data("status");

        if(status == 0){
            var key = '您确定要删除吗？';
        } else {
            var key = '您确定要恢复吗？';
        }

        if(confirm(key)) {
            $.post("{{ path('api_admin_change_status_company') }}",{id:id,status:status},function(data){
                console.log(data);
                if(data.errorCode == 0){

                    //$(".company-table tbody").empty();
                    //getList();
                    popStatus(1, data.message, 1,'', true);

                    // 点击页码
                    $(".mes-page-check").trigger("click");
                } else {
                    popStatus(2, data.message, 1,'', true);
                }
            })

        }
    });

    // 点击编辑
    $('body').on('click','.tab-edit', function() {
        ComMesOpen();
        $('.pop-eqit-mes').show();
        $("input[name='id']").val($(this).data("id"));

        {#var company_id = $(this).data("id");#}
        {#// 获取企业详情#}
        {#$.post("{{ path('api_admin_get_company_detail') }}",{id:company_id},function(data){#}
            {#console.log(data);#}
            {##}
        {#})#}
    });

    //点击取消
    $('.mem-cancel, .close-icon').on('click', function() {
        ComMesClose();
    });

    // 点击编辑保存
    $(".p-first-btn").on('click', function(){
        var param = $('#comMesBtn').serializeObject();

        console.log(param);

        if(param.company_attribute_id == ""){
            popStatus(2, '请选择企业属性', 1,'', true);
            return;
        }
        if(param.company_category_id == ""){
            popStatus(2, '请选择企业类别', 1,'', true);
            return;
        }



        $.post("{{ path('api_admin_edit_company_message') }}",param,function (data) {
            console.log(data);
            ComMesClose();
            if(data.errorCode == 0){
                popStatus(1, data.message, 1,'', true);

                $(".company-table tbody").empty();
                addSearchAction(1);
            } else {
                popStatus(2, data.message, 1,'', true);
            }


        })
    });

    // 科技联络人  ->  查看详情
    $('body').on('click','.table-opera.lia-detail', function() {
        $(".technolog-table tbody").empty();
        ComMesOpen();
        var id = $(this).data("id");
        var he_lia = $('.pop-liaison');

        $.getJSON("{{ path('api_admin_get_company_technological') }}",{id:id},function(data){
            console.log(data);
            var item = data.data;
            for(var i in item){
                var html = '';

                if(item[i].name == null){
                    item[i].name = "";
                }

                html += '<tr>';
                html += '<td class="center-th">'+ item[i].name +'</td>';
                html += '<td>'+ item[i].phone +'</td>';
                html += '<td>'+ item[i].email +'</td>';
                html += '</tr>';

                $(".technolog-table tbody").append(html);
            }


        })

        he_lia.show();
    });

    // 监听input值的变化
    $('.pop-com-input').keyup(function() {
        var this_val_len = $(this).val().length;

        $('.pop-com-num').text(50 - this_val_len);
    });

//    // 选择企业属性，类别
//    $('.enable-btn').on('click', function() {
//        ComMesOpen();
//        $('.pop-classify').show();
//    });
//
//    // 点击导入保存
//    $('.classify-btn').on('click', function() {
//        var param = $('#classSubmit').serializeObject();
//
//        console.log(param);
//    });


    // ajax 上传附件
    var attachment_id = "";
    function uploadFile() {
        var _file = this;
        var data = new FormData();
        data.append('attachment', _file.files[0]);

        URL_fileShareUpload = "{{ path('api_public_upload_attachment') }}";
        $.ajax({
            url:URL_fileShareUpload,
            type: 'POST',
            dataType: 'json',
            processData: false,
            contentType: false,
            data: data,
            success: function (data) {
                if(data.errorCode == 0){

                    $('.upload-finish').html('');

                    attachment_id = data.data.attachment_id;

                    importAction(attachment_id);

//                    var html = '<div class="g-left-list" style="margin: 1px 10px">' +
//                        '<div style="width: 100%;" class="a-line">' +
//                        '<span class="p-name">'+ data.data.name +'</span> ' +
//                        '<span class="p-phone"></span>' +
//                        '</div><span class="reset-icon g-left-icon">' +
//                        '</span>' +
//                        '</div>';
//
//                    $('.upload-finish').html(html);

                } else {
                    popStatus(2,data.message,1,'',true);
                    return false;
                }
            }
        });
    }
    $("#import_file").on("change", uploadFile);

    //导入
    function importAction(file_id){
        $.post("{{ path('admin_import') }}",{id:file_id},function(data){

            if(data.errorCode == 0){
                addSearchAction(1);
                popStatus(1, '上传成功', 1,'', true);
            } else {
                popStatus(2,data.message,2,'',true);
                return false;
            }
        })
    }

    // 导出
    $(".disable-btn").click(function(){

        location.href = "{{ path('admin_export') }}";

    })

    // 导出企业数据参数
    var export_param = {};



    function ComMesClose() {
        $('.pop-status').hide();
        $('.pop-eqit-mes').hide();
        $('.pop-liaison').hide();
        $('.pop-classify').hide();
    }

    function ComMesOpen() {
        $('.pop-status').show();
    }


    // 获取企业信息列表
    var page_times = 0;
    function getList(page,select_credit_code,select_name,select_fr_name,select_user_name,select_company_attribute_id,select_company_category_id,select_admin_id){
        page_times++;

        var param = {page:page};
        param.credit_code = select_credit_code;
        param.name = select_name;
        param.fr_name = select_fr_name;
        param.user_name = select_user_name;
        param.company_attribute_id = select_company_attribute_id;
        param.company_category_id = select_company_category_id;
        param.admin_id = select_admin_id;

        console.log(param);

        export_param = param;

        $.getJSON("{{ path('api_admin_get_company_list') }}",param,function(data){
            $(".company-table tbody").empty(); //3
            console.log(data);
            var list_all_count = data.data.search_all_count;
            var allpage = data.data.all_page;

            // 分页样式
            if(page_times == 1 && allpage > 0){
                pageAction(list_all_count,allpage);
            }

            if(allpage == 0){
                $(".pagination").empty();
            }

            //企业总数
            $(".all-company-count").text(data.data.company_count);

            $(".today-company-count").text(data.data.today_count);


            var item = data.data.data;
            for(var i in item){
                var html = '';
                var key = parseInt(i)+1;

                var list_order_num = parseInt((parseInt(page)-1)*10+key);

                if(item[i].status == 1 ){
                    html += '<tr>';

                } else {
                    html += '<tr class="disable-tr" style="color: #cccccc;">';
                }
                html += '<td class="a-line center-th">'+ list_order_num +'</td>';
                html += '<td class="center-th">'+ item[i].name +'</td>';
                html += '<td class="center-th">'+ item[i].company_attribute +'</td>';
                html += '<td class="center-th">'+ item[i].company_category +'</td>';
                html += '<td class="center-th">'+ item[i].credit_code +'</td>';
                html += '<td class="center-th">'+ item[i].admin +'</td>';

                if(item[i].status == 1){

                    var info_url = "{{ path('company_base_message',{id:'8967543986',type:"1"}) }}".replace('8967543986',item[i].id);

                    html += '<td class="center-th">';
                    html += '<span class="table-opera lia-detail" data-id="'+ item[i].id +'">查看详情</span>';
                    html += '</td>';
                    html += '<td class="center-th">';
                    html += '<a href ="'+ info_url +'">查看详情</a>';
                    html += '</td>';
                    html += '<td class="center-th">';
                    html += '<span class="table-opera tab-edit" data-id="'+ item[i].id +'">编辑 |</span>';
                    html += '<span class="table-opera tab-disable company-status" data-id="'+ item[i].id +'" data-status="0">删除</span>';
                    html += '</td>';
                } else {
                    html += '<td class="center-th">查看详情</td>';
                    html += '<td class="center-th">查看详情</td>';
                    html += '<td class="center-th">';
                    html += '<span class="table-opera tab-enable company-status" data-id="'+ item[i].id +'" data-status="1">恢复</span>';
                    html += '</td>';
                }

                html += '</tr>';
                $(".company-table tbody").append(html);
            }
        })
    }

    addSearchAction(1);  //4

    //5
    function addSearchAction(page){

        getList(page,select_credit_code,select_name,select_fr_name,select_user_name,select_company_attribute_id,select_company_category_id,select_admin_id);
    }


</script>
{% endblock %}